<template>
  <comcard>
    <div slot="com_title">账户列表</div>
    <div slot="com_content">
      <el-table :data="dataSource" style="width: 100%">
        <el-table-column label="创建日期">
          <template prop="ctime" #default="{ row }">
            {{ row.ctime | timeMF }}
          </template>
        </el-table-column>
        <el-table-column prop="account" label="账户"> </el-table-column>
        <el-table-column prop="userGroup" label="用户组"> </el-table-column>
        <el-table-column lable="操作">
          <template slot-scope="{ row }">
            <el-button type="danger" @click="del(row.id)">删除</el-button>
            <el-button type="warning" @click="update(row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改弹窗 -->
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="ruleForm" label-width="70px">
          <el-form-item label="账户">
            <el-input v-model="ruleForm.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="活动区域">
            <el-select
              v-model="ruleForm.userGroup"
              placeholder="请选择活动区域"
            >
              <el-option label="超级管理员" value="超级管理员"></el-option>
              <el-option label="普通管理员" value="普通管理员"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="doupdate"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </comcard>
</template>

<script>
import Comcard from "../../components/comcard";
import { api_delete, api_query, api_update } from "../../api/users.js";
export default {
  components: {
    Comcard,
  },
  data() {
    return {
      dataSource: [],
      searchObj: {
        currentPage: 1,
        pageSize: 2,
      },
      dialogFormVisible: false,
      ruleForm: {
        account: "",
        userGroup: "",
        id:""
      },
      total:0
    };
  },
  mounted() {
    this.query();
  },

  methods: {
    async query() {
      let rst = await api_query(this.searchObj);
      let { total, data } = rst;
       this.total = total;
      this.dataSource = data;
      console.log(rst);
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let rst = await api_delete({ id });
          let { code, msg } = rst;
          if (code == 0) {
            this.query();
          }
          console.log(rst);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchObj.pageSize = val;
      this.query();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.searchObj.currentPage = val;
      this.query();
    },
    update(row) {
      let { id, account, userGroup } = row;
      this.ruleForm = { account, userGroup, id };
      this.dialogFormVisible = true;
    },
    async doupdate(){
      let rst= await api_update(this.ruleForm);
      let{code,msg}=rst;
      if(code==0){
        this.query();
        this.dialogFormVisible=false;
      }
    }
  },
};
</script>

<style lang="less" scoped></style>
